package com.ibeetl.admin.console.service;


import com.ibeetl.admin.console.dao.CoreDataDao;
import com.ibeetl.admin.console.dao.CoreOrdersDao;
import com.ibeetl.admin.console.dao.CoreProductDao;
import com.ibeetl.admin.console.entity.CoreOrders;
import com.ibeetl.admin.console.entity.CoreProduct;
import com.ibeetl.admin.console.model.DataMap;
import com.ibeetl.admin.core.service.BaseService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CoreDataService extends BaseService<CoreOrders> {

    private final Log log = LogFactory.getLog(this.getClass());

    @Autowired
    private CoreDataDao dataDao;

    @Autowired
    private CoreProductDao productDao;

    public HashMap queryOrderDataByDate(Integer day) {
        List<CoreOrders> coreOrdersList = dataDao.queryOrderDataByDate(day);
        List<Integer> productIdLsit = coreOrdersList.stream().map(t -> t.getProductId()).collect(Collectors.toList());

        HashMap<Integer, String> prodectIdToNameMap = new HashMap<>();
        for (Integer id : productIdLsit) {
            String name = productDao.getProductNameById(id);
            prodectIdToNameMap.put(id, name);
        }
        List<String> prodectNameList = (List<String>) prodectIdToNameMap.values().stream().distinct().collect(Collectors.toList());

        HashMap<String, String> freqMap = new HashMap<>();
        for (CoreOrders orders : coreOrdersList) {
            String name = prodectIdToNameMap.get(orders.getProductId());

            if (!freqMap.containsKey(name)) {
                freqMap.put(name, orders.getCount());
            } else {
                Integer count = Integer.parseInt(freqMap.get(name)) + Integer.parseInt(orders.getCount());
                String strCount = count.toString();
                log.info(strCount);
                freqMap.put(name, strCount);
            }
        }

        List<DataMap> dataMapList = new ArrayList<>();
        for (HashMap.Entry entry : freqMap.entrySet()) {
            DataMap dataMap = new DataMap();
            dataMap.setName(entry.getKey().toString());
            dataMap.setValue(Integer.valueOf(entry.getValue().toString()));
            dataMapList.add(dataMap);
        }

        HashMap resultMap = new HashMap();
        resultMap.put("name", prodectNameList);
        resultMap.put("dataMap", dataMapList);

        return resultMap;
    }

    public HashMap querySaleRateByDate(Integer day) {
        List<CoreOrders> coreOrdersList = dataDao.queryOrderDataByDate(day);
        List<Integer> productIdLsit = coreOrdersList.stream().map(t -> t.getProductId()).collect(Collectors.toList());

        HashMap<Integer, String> prodectIdToNameMap = new HashMap<>();
        for (Integer id : productIdLsit) {
            String name = productDao.getProductNameById(id);
            prodectIdToNameMap.put(id, name);
        }
        List<String> prodectNameList = (List<String>) prodectIdToNameMap.values().stream().distinct().collect(Collectors.toList());

        HashMap<String, String> freqMap = new HashMap<>();
        for (CoreOrders orders : coreOrdersList) {
            String name = prodectIdToNameMap.get(orders.getProductId());

            if (!freqMap.containsKey(name)) {
                Integer count = Integer.parseInt(orders.getCount());
                Integer price = Integer.parseInt(orders.getDealPrice());
                Integer sale = count * price;
                freqMap.put(name, sale.toString());
            } else {
                Integer count = Integer.parseInt(orders.getCount());
                Integer price = Integer.parseInt(orders.getDealPrice());
                Integer sale = count * price;
                Integer newCount = Integer.parseInt(freqMap.get(name)) + sale;
                String strCount = newCount.toString();
                log.info(strCount);
                freqMap.put(name, strCount);
            }
        }


        HashMap resultMap = new HashMap();
        resultMap.put("name", freqMap.keySet());
        resultMap.put("sale", freqMap.values());

        return resultMap;
    }

}
